var Mes_matieres_premieres = {
    
    init: function() {
        
        /***********************************************************************
         * Variables
         **********************************************************************/
        var TableMatiere_premiere = $('table#liste_matiere_premiere'),
        LinkCreate = $('a.create'),
        LinkUpdate = TableMatiere_premiere.find('a.update'),
        LinkDelete = TableMatiere_premiere.find('a.delete'),
        LinkMore = TableMatiere_premiere.find('a.more'),
        LinkAddInStock = TableMatiere_premiere.find('a.add-in-stock'),
        LinkRemoveFromStock = TableMatiere_premiere.find('a.remove-from-stock'),
        EditMatiere_premiere = $('div#EditMatiere_premiere'),
        DeleteMatiere_premiere = $('div#DeleteMatiere_premiere'),
        urlDelete;
        
        /***********************************************************************
         * Forms
         **********************************************************************/
        EditMatiere_premiere.dialog({
            autoOpen: false,
            resizable: false,
            width: 500,
            height: 400,
            modal: true,
            buttons: {
                "Valider": function() {
                    var formData = {};
                    var ARTICLE_ID = EditMatiere_premiere.find('input[type=hidden]#ARTICLE_ID').val();
                    var ARTICLE_REF = EditMatiere_premiere.find('input[type=text]#ARTICLE_REF').val();
                    var ARTICLE_LIBELLE = EditMatiere_premiere.find('input[type=text]#ARTICLE_LIBELLE').val();
                    var ARTICLE_SEUIL = EditMatiere_premiere.find('input[type=text]#ARTICLE_SEUIL').val();
                    var UNITE_ID = EditMatiere_premiere.find('select#UNITE_ID').val();
                    var url = EditMatiere_premiere.find('form').attr('action');

                    if (ARTICLE_ID != '') {
                        formData.ARTICLE_ID = ARTICLE_ID;
                    }
                    formData.ARTICLE_REF = ARTICLE_REF;
                    formData.ARTICLE_LIBELLE = ARTICLE_LIBELLE;
                    formData.ARTICLE_SEUIL = ARTICLE_SEUIL;
                    formData.UNITE_ID = UNITE_ID;
                    
                    $.ajax({
                        type: "GET",
                        url: url,
                        data: formData,
                        async: false,
                        dataType: 'json',
                        success: function(matiere_premiere){
                            UpdateList(matiere_premiere);
                            EditMatiere_premiere.dialog('close');
                        }
                    });
                },
                Cancel: function() {
                    EditMatiere_premiere.dialog('close');
                }
            },
            close: function() {
                EditMatiere_premiere.html('');
            }
        });
        
        DeleteMatiere_premiere.dialog({
            autoOpen: false,
            resizable: false,
            width: 'auto',
            height: 'auto',
            modal: true,
            buttons: {
                "Valider": function() {
                    $.ajax({
                        type: "GET",
                        url: urlDelete,
                        async: true,
                        dataType: 'json',
                        success: function(matiere_premieres){
                            UpdateList(matiere_premieres);
                            DeleteMatiere_premiere.dialog('close');
                        }
                    });
                },
                Cancel: function() {
                    DeleteMatiere_premiere.dialog('close');
                }
            }
        });
        
        /***********************************************************************
         * Methods on events
         **********************************************************************/
        function OnCreate(event, element) {
            event.preventDefault();
            var url = $(element).attr('href');
            EditMatiere_premiere.dialog('open').load(url);
        }
        
        function OnUpdate(event, element) {
            event.preventDefault();
            var url = $(element).attr('href');
            alert(url);
        }
        
        function OnDelete(event, element) {
            event.preventDefault();
            var url = $(element).attr('href');
            alert(url);
        }
        
        function OnAddingInStock(event, element) {
            event.preventDefault();
            var url = $(element).attr('href');
            
            titre = 'Ajout de Stock';
            $('div.dialog').dialog({
                title:titre,
                autoOpen:false,
                width: 500,
                height: 'auto',
                modal:true,
                buttons: {
                    'Valider' : function(e) {
                        addStock(url);
                    },
                    'Annuler' : function(e) {
                        $(this).dialog('close');
                    }
                }
            })
            .load(url);
            $('div.dialog').dialog('open');
        }
        
        function OnRemovingFromStock(event, element) {
            event.preventDefault();
            var url = $(element).attr('href');
            var qty = $(element).siblings('input[name=RET_STOCK]').val();
            
            if(confirm('Confirmez le retrait du stock de '+qty+' éléments.')) {
                $.ajax({
                    url : url,
                    type: "POST",
                    data: {
                        quantity : qty
                    },
                    success: function(e) {
                        $(location).attr('href',$(location).attr('href'));
                    }
                });
            }
        }

        function UpdateList(matiere_premieres) {
            var table = $('table#liste_matiere_premiere'),
            tbody = table.find('tbody'),
            base_url = $('input[type=hidden]#base_url').val();
        
            tbody.empty();

            $.each(matiere_premieres, function (index, matiere_premiere){
                var _update = '<a href="' + base_url + 'matiere_premiere/update?ARTICLE_ID=' + matiere_premiere.ARTICLE_ID + '" class="btn update">Modifier</a>';
                var _delete = '<a href="' + base_url + 'matiere_premiere/delete?ARTICLE_ID=' + matiere_premiere.ARTICLE_ID + '" class="btn delete">Supprimer</a>';

                var tr = '<tr id="' + matiere_premiere.ARTICLE_ID + '">';
                tr = tr + '<td>' + matiere_premiere.ARTICLE_REF + '</td>'
                tr = tr + '<td>' + matiere_premiere.ARTICLE_LIBELLE + '</td>'
                tr = tr + '<td>' + matiere_premiere.UNITE_CODE + '</td>'
                tr = tr + '<td>' + matiere_premiere.STOCK + '</td>'
                tr = tr + '<td><form class="form-inline"><input type="text" class ="input-small" name="RET_STOCK" id="RET_STOCK" />';
                tr = tr + '<a class="btn"> Retirer</a></form></td>';
                tr = tr + '<td><form class="form-inline">';
                tr = tr + '<input type="text" class ="input-small" name="AJ_STOCK" id="AJ_STOCK" /> <a class="btn"> Ajouter stock</a></form></td>';
                tr = tr + '<td><form class="form-inline">' + _update + '  ' + _delete +  '</form></td>'
                tr = tr + '</tr>';

                table.append(tr);
            });
        
            table.find('a.create').click(function(event){
                OnCreate(event, $(this));
            });
            table.find('a.update').click(function(event){
                OnUpdate(event, $(this));
            });
            table.find('a.delete').click(function(event){
                OnDelete(event, $(this));
            });
        }
        
        function OnLoad() {
            var lines = TableMatiere_premiere.find('tr').not('thead tr');
            
            $.each(lines, function(index, line){
                var STOCK = $(line).find('input[type=hidden][name=STOCK]'),
                RET_STOCK_INPUT = $(line).find('input[name=RET_STOCK]'),
                RET_STOCK_LINK = RET_STOCK_INPUT.siblings('a');
                
                if(STOCK.val() == 0) {
                    RET_STOCK_INPUT.attr('disabled', true);
                    RET_STOCK_LINK.addClass('disabled');
                }
            });
        }
        
        /***********************************************************************
         * Events
         **********************************************************************/
        LinkCreate.on('click', function(event) {
            OnCreate(event, $(this));
        });
        
        LinkUpdate.on('click', function(event) {
            OnUpdate(event, $(this));
        });
        
        LinkDelete.on('click', function(event) {
            OnDelete(event, $(this));
        });
        
        LinkAddInStock.on('click', function(event) {
            OnAddingInStock(event, $(this));
        });
        
        LinkRemoveFromStock.on('click', function(event) {
            OnRemovingFromStock(event, $(this));
        });
        
        OnLoad();
    }
    
};

function addStock(url) {    
    var empl = $('#addStockEmplacement').val();
    var qte = $('#addStockQuantite').val();
    
    $.ajax({
        url : url,
        type: 'POST',
        data: {
            addStock : true,
            emplacement : empl,
            quantite : qte
        },
        success : function() {
            $(location).attr('href', $(location).attr('href'));
        }
    });
}

$(function(){
    
    $(document).ready(function(){
        
        Mes_matieres_premieres.init();
        
    });
    
});

